(function($, ajax){
    var ajaxButton = function (button, callback) {
        var $button=$(button);
        if (typeof callback == 'function') {
            callback = { success: callback };
        }
        if (typeof callback.always == 'undefined') {
            callback.always=function () {
                $button.button('reset');
            }
        }

        $button.click(function (e) {
            e.preventDefault();
            $button.button('loading');
            return ajax.button($button, callback);
        })
    };

    $("[lk-role=ajax-button]").each(function () {
        ajaxButton($(this), function (ret) {
            $(this).text(ret.message);
        });
    })

    var ajaxForm = function (form, callback) {
        var $form = $(form);
        console.log($form);
        if (typeof callback == 'function') {
            callback = { success: callback };
        }
        if (typeof callback.always == 'undefined') {
            callback.always=function () {
                $form.find("[type=submit]").button('reset');
            }
        }

        $form.submit(function (e) {
            e.preventDefault();
            $form.find("[type=submit]").button("loading");
            ajax.form($form, callback)
        })
    };

    $("[lk-role=ajax-form]").each(function () {
        ajaxForm($(this), function () {
            location.reload();
        });
    })

    $.ajaxUI = {
        // 从特殊的button提交
        button: function (button, callback) {
            var $button=$(button);
            $button.each(function () {
                ajaxButton($(this), callback);
            })
        },
        form: ajaxForm,
    }
})(jQuery, $.myAjax);